const router = require('koa-router')()
const {postUserAndRole, getUserAndRole, patchUserAndRole, getUserAndRoleById} = require('../../controller/userAndRole')

router.prefix('/api/userAndRole')

router.get('/userAndRole', async (ctx, next) => {
  const {userName, roleName, pageIndex, pageSize} = ctx.query
  ctx.body = await getUserAndRole({userName, roleName, pageIndex, pageSize})
})

router.get('/userAndRole/:id', async (ctx, next) => {
  const {id} = ctx.params
  ctx.body = await getUserAndRoleById(id)
})

router.post('/userAndRole', async (ctx, next) => {
  const {uid, rid} = ctx.request.body
  ctx.body = await postUserAndRole({uid, rid})
})

router.patch('/userAndRole', async (ctx, next) => {
  const {rid, id} = ctx.request.body
  ctx.body = await patchUserAndRole({rid, id})
})

module.exports = router
